﻿using System;
using System.Collections.Generic;
using System.Text;
using BCCommon.Enums;
using BCCommon.PaymentEnums;
using BCDto.Trading.SaleContract;
using BCService.Common.Payment.Payment.AliPayModel;
using BCService.Common.Payment.Payment.AliPayModel.Trade.Precreate;
using BCService.Common.Payment.PaymentRecord;
using BCService.Trading.SaleContract;
using BCService.Utils;
using Microsoft.Extensions.Configuration;

namespace BCService.Common.Payment.Payment.AliPayBusiness
{
    public class AliPayBusinessService : IAliPayBusinessService
    {
        private readonly IConfiguration configuration;
        private readonly ISaleContractService saleContractService;
        public AliPayBusinessService(IConfiguration configuration,ISaleContractService saleContractService)
        {
            this.configuration = configuration;
            this.saleContractService = saleContractService;
        }

        public IAlipayBizContent AdditionBusinessBizContent(AlipayBizContent bizContent, string alipayBusinessType,FlowType flowType, string referenceNumber)
        {
            IAlipayBizContent alipayBizContent = bizContent;
            switch (alipayBusinessType)
            {
                case "alipay.trade.app.pay":
                    {

                    }
                    break;
                case "alipay.trade.page.pay":
                    {

                    }
                    break;
                case "alipay.trade.precreate":
                    {
                        alipayBizContent = bizContent.TransformTo<AlipayBizContentTradePrecreate>(onAfter:(_,dest)=> {
                            if (flowType == FlowType.Shopping) {
                                var saleContract = this.GetSaleContractInfo(referenceNumber);
                                dest.Subject = string.Format("{0}_购物_订单编号-{1}", configuration["WebsiteTitle"],saleContract.SaleContractNo);
                                List<GoodsDetailInfo> goodsDetails = new List<GoodsDetailInfo>();
                                foreach(var item in saleContract.Checklist)
                                {
                                    GoodsDetailInfo goods = new GoodsDetailInfo
                                    {
                                        GoodsId = string.Format("{0}-{1}",item.SaleGoodsId, item.SaleGoodsNormId),
                                        GoodsName = string.Format("{0}_{1}", item.GoodsName, item.NormName),
                                        Quantity = item.SaleCount,
                                        Price = item.SaleAmount,
                                        GoodsCategory = item.CategoryName,
                                        CategoriesTree = item.SaleGoodsCategoryId.ToString(),
                                        Body = string.Format("{0}_{1}", item.GoodsName, item.NormName),
                                        ShowUrl = string.Empty
                                    };
                                    goodsDetails.Add(goods);
                                }
                                dest.GoodsDetail = goodsDetails;
                                dest.MerchantOrderNo = saleContract.SaleContractNo;
                                dest.QrCodeTimeoutExpress = "7d";
                            }
                        });
                    }
                    break;
                case "alipay.fund.trans.toaccount.transfer":
                    {

                    }
                    break;
                case "alipay.trade.refund":
                    {

                    }
                    break;
                case "alipay.data.dataservice.bill.downloadurl.query":
                    {

                    }
                    break;
                case "alipay.trade.query":
                    {

                    }
                    break;
                default:
                    break;
            }
            return alipayBizContent;
        }
        /// <summary>
        /// 获取销售合同信息
        /// </summary>
        /// <param name="referenceNumber">销售订单编号</param>
        /// <returns></returns>
        private SaleContractDto GetSaleContractInfo(string referenceNumber)
        {
            if(!long.TryParse(referenceNumber, out long saleContractId))
            {
                throw new ArgumentException("此订单信息不正确!");
            }

            var saleContractDto = this.saleContractService.Get(saleContractId);
            if (saleContractDto == null)
            {
                throw new ArgumentException("此订单信息不存在!");
            }

            return saleContractDto;
        }
    }
}
